package cn.lystudio.rohm_time_op.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PageUtil;
import cn.lystudio.rohm_time_op.entity.dto.TopDeptDTO;
import cn.lystudio.rohm_time_op.entity.po.DepartmentsPO;
import cn.lystudio.rohm_time_op.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohm_time_op.exceptions.JsonException;
import cn.lystudio.rohm_time_op.service.DepartmentService;
import lombok.extern.apachecommons.CommonsLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/topDept")
@Slf4j
public class TopDeptController {
    private DepartmentService departmentService;

    @Autowired
    @Lazy
    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @SaCheckLogin
    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page,Integer limit){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if(roleList.size()!=1) throw new JsonException(500,"权限异常");
        if(!(roleList.get(0).equalsIgnoreCase("1"))) throw new JsonException(500,"无此权限");
        PageUtil.setFirstPageNo(1);
        List<TopDeptDTO> topDeptList = departmentService.getAll4TopDept();
        if(CollUtil.isEmpty(topDeptList)) return LayuiPageApiResponse.ofSuccessData(null,0);

        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,topDeptList),topDeptList.size());
    }

    @SaCheckLogin
    @PostMapping("/uptEnabled")
    public SaResult uptEnabled(Integer departmentID, Boolean enabled){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if(roleList.size()!=1) throw new JsonException(500,"权限异常");
        if(!(roleList.get(0).equalsIgnoreCase("1"))) throw new JsonException(500,"无此权限");

        return SaResult.data(departmentService.uptDeptEnabled(departmentID, enabled));
    }

    @SaCheckLogin
    @PostMapping("/delTopDept")
    public SaResult delTopDept(Integer departmentID){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if(roleList.size()!=1) throw new JsonException(500,"权限异常");
        if(!(roleList.get(0).equalsIgnoreCase("1"))) throw new JsonException(500,"无此权限");
        return SaResult.data(departmentService.delDept(departmentID));
    }

    @SaCheckLogin
    @PostMapping("/addTopDept")
    public SaResult addTopDept(@RequestBody DepartmentsPO departmentsPO){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if(roleList.size()!=1) throw new JsonException(500,"权限异常");
        if(!(roleList.get(0).equalsIgnoreCase("1"))) throw new JsonException(500,"无此权限");
        departmentsPO.setParentID(0);
        return SaResult.data(departmentService.addDept(departmentsPO));
    }

    @SaCheckLogin
    @PostMapping("/uptTopDept")
    public SaResult uptTopDept(@RequestBody DepartmentsPO departmentsPO){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if(roleList.size()!=1) throw new JsonException(500,"权限异常");
        if(!(roleList.get(0).equalsIgnoreCase("1"))) throw new JsonException(500,"无此权限");
        return SaResult.data(departmentService.uptDept(departmentsPO));
    }

    @SaCheckLogin
    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/getAllTopDeptName")
    public SaResult getAllTopDeptName() {
        List<TopDeptDTO> all4TopDept = departmentService.getAll4TopDept();
        CollUtil.filter(all4TopDept, TopDeptDTO::getEnabled);
        return SaResult.data(all4TopDept);
    }


}
